package com.devenv.repository;

import com.devenv.entity.SystemConfig;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 系统配置数据访问层
 * <p>
 * 提供系统配置实体的数据库访问操作
 * </p>
 * 
 * @author DevEnv OpenSource Team
 * @version 1.0.0
 * @since 1.0.0
 */
@Repository
public interface SystemConfigRepository extends JpaRepository<SystemConfig, Long> {

    /**
     * 根据配置键查找配置
     */
    Optional<SystemConfig> findByConfigKey(String configKey);

    /**
     * 检查配置键是否存在
     */
    boolean existsByConfigKey(String configKey);

    /**
     * 根据配置类型查找配置
     */
    List<SystemConfig> findByConfigType(SystemConfig.ConfigType configType);

    /**
     * 根据配置分组查找配置
     */
    List<SystemConfig> findByConfigGroupOrderBySortOrderAsc(String configGroup);

    /**
     * 查找所有公开配置
     */
    List<SystemConfig> findByIsPublicTrueOrderByConfigGroupAscSortOrderAsc();

    /**
     * 查找所有非系统配置（可删除的配置）
     */
    List<SystemConfig> findByIsSystemFalseOrderByConfigGroupAscSortOrderAsc();

    /**
     * 分页查询配置
     */
    Page<SystemConfig> findByConfigKeyContainingOrDescriptionContaining(String configKey, String description, Pageable pageable);

    /**
     * 根据配置组分页查询
     */
    Page<SystemConfig> findByConfigGroupOrderBySortOrderAsc(String configGroup, Pageable pageable);

    /**
     * 查找所有配置分组
     */
    @Query("SELECT DISTINCT c.configGroup FROM SystemConfig c WHERE c.configGroup IS NOT NULL ORDER BY c.configGroup")
    List<String> findAllConfigGroups();

    /**
     * 统计配置数量按分组
     */
    @Query("SELECT c.configGroup, COUNT(c) FROM SystemConfig c GROUP BY c.configGroup ORDER BY c.configGroup")
    List<Object[]> countConfigsByGroup();

    /**
     * 查找指定分组的公开配置
     */
    @Query("SELECT c FROM SystemConfig c WHERE c.configGroup = :group AND c.isPublic = true ORDER BY c.sortOrder ASC")
    List<SystemConfig> findPublicConfigsByGroup(@Param("group") String group);

    /**
     * 根据配置键批量查询
     */
    @Query("SELECT c FROM SystemConfig c WHERE c.configKey IN :keys")
    List<SystemConfig> findByConfigKeyIn(@Param("keys") List<String> keys);

    /**
     * 查找系统核心配置
     */
    List<SystemConfig> findByIsSystemTrueOrderByConfigGroupAscSortOrderAsc();

    /**
     * 搜索配置（按键或描述）
     */
    @Query("SELECT c FROM SystemConfig c WHERE " +
           "(c.configKey LIKE %:keyword% OR c.description LIKE %:keyword%) AND " +
           "(:group IS NULL OR c.configGroup = :group) " +
           "ORDER BY c.configGroup ASC, c.sortOrder ASC")
    List<SystemConfig> searchConfigs(@Param("keyword") String keyword, @Param("group") String group);

    /**
     * 查找默认值为空的配置
     */
    @Query("SELECT c FROM SystemConfig c WHERE c.configValue IS NULL OR c.configValue = ''")
    List<SystemConfig> findConfigsWithEmptyValues();

    /**
     * 根据更新时间查找最近修改的配置
     */
    @Query("SELECT c FROM SystemConfig c WHERE c.updatedAt > :since ORDER BY c.updatedAt DESC")
    List<SystemConfig> findRecentlyUpdatedConfigs(@Param("since") java.time.LocalDateTime since);
}
